/*
 * Created by JFormDesigner on Sat Dec 26 13:55:24 CST 2020
 */

package cn.fourhundredandeighteen.view.AdminView.RewardView;

import java.awt.event.*;
import cn.fourhundredandeighteen.controller.SubjectController;
import cn.fourhundredandeighteen.entity.Student;
import cn.fourhundredandeighteen.entity.Subject;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.view.AdminView.AllcourseFind.UpDate.upData;
import cn.fourhundredandeighteen.view.AdminView.RewardView.addReward.addRewardInfo;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.Component.Container;

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * @author Br.S
 */
public class rewardView extends JPanel {
    private SubjectController subjectController = new SubjectController();
    public rewardView() {
        initComponents();
        getdata();
    }
    public void getdata(){
        Result<List<Subject>> allSubjectInfo = subjectController.getAllSubjectInfo();
          this.add(viewSubjectTable(allSubjectInfo.getData()));
          this.updateUI();
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here

    }

    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
        try {
            Result result = subjectController.delSubject(Long.valueOf(textField1.getText()));
            if (result.getMassage()=="SUCCESS"){
                JOptionPane.showMessageDialog(null, "删除成功", "", JOptionPane.DEFAULT_OPTION);
                Container.getInstance().upDatePage();
            }
        }catch (NumberFormatException numberFormatException){
            JOptionPane.showMessageDialog(null,"请先选择课程");
        }
    }

    private void button3ActionPerformed(ActionEvent e) {
        // TODO add your code here
        JFrame jFrame = new JFrame();
        try{
            rewardInfoChang rewardInfoChang = new rewardInfoChang(Long.valueOf(textField1.getText()));
            rewardInfoChang.setVisible(true);
            jFrame.setBounds(100,100,550,560);
            jFrame.add(rewardInfoChang);
            jFrame.setVisible(true);
        }catch (NumberFormatException numberFormatException){
            JOptionPane.showMessageDialog(null, "请先输入id");
        }
    }

    private void button1MouseClicked(MouseEvent e) {
        // TODO add your code here
        try {
            Result<List<Student>> subjectUsers = subjectController.getSubjectUsers(Long.valueOf(textField1.getText()));
            JScrollPane jScrollPan = viewStudentTable(subjectUsers.getData());
            JOptionPane.showMessageDialog(null, jScrollPan, "", JOptionPane.DEFAULT_OPTION);
            Container.getInstance().upDatePage();
        }catch (NumberFormatException numberFormatException){
            JOptionPane.showMessageDialog(null,"请先选择课程");
        }

    }

    private void button4ActionPerformed(ActionEvent e) {
        // TODO add your code here


    }

    public JScrollPane viewSubjectTable(List<Subject> data){
        String[] name={"id","比赛名称","获奖级别","比赛级别","比赛类别","人数","比赛时间","修改时间"};
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
        if (data==null){
            return jScrollPane( new Object[0][8],name);
        }
        Object[][] tableData=new Object[data.size()][8];
        for (int i = 0; i < data.size(); i++) {
            tableData[i][0]= data.get(i).getSubjectId();
            tableData[i][1]= data.get(i).getSubjectName();
            tableData[i][2]= data.get(i).getSubjectGrade();
            tableData[i][3]= data.get(i).getSubjectLevel();
            tableData[i][4]= data.get(i).getSubjectCategory();
            tableData[i][5]= data.get(i).getSubjectCount();
            tableData[i][6]= dateFormat.format(data.get(i).getSubjectTime());
            tableData[i][7]= dateFormat.format(data.get(i).getUpdateTime());
        }
        return jScrollPane(tableData, name);
    }
    public JScrollPane viewStudentTable(List<Student> data){
        String[] name={"学号:", "名字:", "密码:", "性别:", "年龄:", "专业:", "班级:", "更新时间:"};
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
        if (data==null){
            return jScrollPane( new Object[0][8],name);
        }
        Object[][] tableData=new Object[data.size()][8];
        for (int i = 0; i < data.size(); i++) {
            tableData[i][0]= data.get(i).getStudentId();
            tableData[i][1]= data.get(i).getStudentName();
            tableData[i][2]= data.get(i).getStudentPassword();
            tableData[i][3]= data.get(i).getStudentGender();
            tableData[i][4]= data.get(i).getStudentAge();
            tableData[i][5]= data.get(i).getStudentMajor();
            tableData[i][6]= data.get(i).getStudentClass();
            tableData[i][7]= dateFormat.format(data.get(i).getUpdateTime());
        }
        return jScrollPane(tableData, name);
    }
    private  JScrollPane jScrollPane(Object[][] tableData,String[] name){
        JTable table=new JTable(tableData,name);
        table.setSize(1000,800);
        table.setBackground(Color.WHITE);
        table.setRowHeight(32);//设置行高
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class, r);
        // 设置表格内容颜色
        table.getColumnModel().getColumn(6).setPreferredWidth(100);
        table.getColumnModel().getColumn(7).setPreferredWidth(100);
        table.setForeground(new Color(47,53,66));                   // 字体颜色
        table.setFont(new Font(null, Font.PLAIN, 15));      // 字体样式
        table.setSelectionForeground(new Color(236,240,241));      // 选中后字体颜色
        table.setSelectionBackground(new Color(87,96,111));     // 选中后字体背景
        table.setGridColor(new Color(87,96,111));                     // 网格颜色
        // 设置表头

        Dimension size = table.getTableHeader().getPreferredSize();
        size.height = 40;//设置新的表头高度32
        table.getTableHeader().setPreferredSize(size);
        table.getTableHeader().setFont(new Font(null, Font.BOLD, 15));// 设置表头名称字体样式
        table.getTableHeader().setForeground(Color.white);// 设置表头名称字体颜色
        table.getTableHeader().setResizingAllowed(false);// 设置不允许手动改变列宽
        table.getTableHeader().setReorderingAllowed(false);// 设置不允许拖动重新排序各列
        table.getTableHeader().setBackground(new Color(47,53,66));
        ((DefaultTableCellRenderer)table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);//居中
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(770,568));
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.getHSBColor(0.0000F,0.0000F,0.9333F)));
        scrollPane.setVisible(true);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e)
            {
                if (e.getValueIsAdjusting()){
                    textField1.setText(table.getValueAt(table.getSelectedRow(), 0).toString());
                }
            }
        });
        return scrollPane;
    }

    private void button4MouseClicked(MouseEvent e) {
        // TODO add your code here
        try {
            JFrame jFrame = new JFrame();
            addRewardInfo addRewardInfo = new addRewardInfo();
            addRewardInfo.setVisible(true);
            jFrame.setBounds(100,100,550,560);
            jFrame.add(addRewardInfo);
            jFrame.setVisible(true);
        }catch (NumberFormatException numberFormatException){
            JOptionPane.showMessageDialog(null,"请先选择课程");
        }
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label2 = new JLabel();
        textField1 = new JTextField();
        button1 = new Button();
        button2 = new Button(Button.CANCELBUTTON);
        button3 = new Button();
        button4 = new Button();

        //======== this ========
        setLayout(new BorderLayout());

        //======== panel1 ========

        {
            panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 26, 15));

            //---- label2 ----
            label2.setText("\u8bf7\u8f93\u5165\u53f7");
            label2.setIconTextGap(5);
            panel1.add(label2);

            //---- textField1 ----
            textField1.setColumns(7);
            panel1.add(textField1);

            //---- button1 ----
            button1.setText("\u83b7\u5956\u8be6\u60c5");
            button1.addActionListener(e -> {
			button1ActionPerformed(e);
			button1ActionPerformed(e);
		});
            button1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button1MouseClicked(e);
                }
            });
            panel1.add(button1);

            //---- button2 ----
            button2.setText("\u83b7\u5956\u4fe1\u606f\u5220\u9664");
            button2.addActionListener(e -> button2ActionPerformed(e));
            panel1.add(button2);

            //---- button3 ----
            button3.setText("\u83b7\u5956\u4fe1\u606f\u4fee\u6539");
            button3.addActionListener(e -> button3ActionPerformed(e));
            panel1.add(button3);

            //---- button4 ----
            button4.setText("\u83b7\u5956\u4fe1\u606f\u65b0\u589e");
            button4.addActionListener(e -> button4ActionPerformed(e));
            button4.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button4MouseClicked(e);
                }
            });
            panel1.add(button4);
        }
        add(panel1, BorderLayout.NORTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label2;
    private JTextField textField1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
